******************************************************************************************************
********** THIS .DO FILE PRODUCES TABLE 3 (RD Estimates of the effect of a left-wing mayor) **********
******************************************************************************************************

clear *

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1

********* Perform (robust bias-corrected) RD estimates and produce results table **********

foreach outcome in tot_exp_avg_pc 	 		///
				   tot_exp_avg_sgdp  		///
				   tot_rev_avg_pc    		///
				   tot_rev_avg_sgdp  		///
				   current_exp_avg_share 	///
				   investments_avg_share 	///
				   personnel_avg_share 		///
				   social_exp_avg_share 	///
				   social_exp_avg_pc 		///
				   health_avg_share 		///
				   education_avg_share 		///
				   welfare_avg_share 		///
				   nonsocial_exp_avg_share  ///
				   housing_avg_share 		///
				   transport_avg_share 		///
				   other_avg_share 			{

	*** (1) baseline (whole sample, average over 4-years, CCT 2014 MSE-optimal bandwidth)
	qui rdrobust res_`outcome' margin_mayor_left, vce(cluster mun_code) all

	local coeff1_`outcome' : di %9.2f (e(tau_bc))
	local se1_`outcome'    : di %9.2f (e(se_tau_rb))
	local pv1_`outcome'    : di %9.2f (e(pv_rb))
	local n1_`outcome' 	   = e(N)
	local effn1_`outcome'  = e(N_h_l) + e(N_h_r)

	*** (2) lame-ducks subsample (4-years average)
	qui rdrobust res_`outcome' margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all

	local coeff2_`outcome'  : di %9.2f (e(tau_bc))
	local se2_`outcome' 	: di %9.2f (e(se_tau_rb))
	local pv2_`outcome' 	: di %9.2f (e(pv_rb))
	local n2_`outcome' 		= e(N)
	local effn2_`outcome' 	= e(N_h_l) + e(N_h_r)


	*** (3) Tiebout-below-median subsample (4-years average)
	qui rdrobust res_`outcome' margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all

	local coeff3_`outcome'  : di %9.2f (e(tau_bc))
	local se3_`outcome' 	: di %9.2f (e(se_tau_rb))
	local pv3_`outcome' 	: di %9.2f (e(pv_rb))
	local n3_`outcome' 		= e(N)
	local effn3_`outcome' 	= e(N_h_l) + e(N_h_r)


	*** (4) Tiebout-below-75th percentile subsample (4-years average)
	qui rdrobust res_`outcome' margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all

	local coeff4_`outcome'  : di %9.2f (e(tau_bc))
	local se4_`outcome' 	: di %9.2f (e(se_tau_rb))
	local pv4_`outcome'     : di %9.2f (e(pv_rb))
	local n4_`outcome' 		= e(N)
	local effn4_`outcome' 	= e(N_h_l) + e(N_h_r)


*** (5) Positive Oil-windfalls subsample
	qui rdrobust res_`outcome' margin_mayor_left if oil_sample == 1, vce(cluster mun_code) all

	local coeff5_`outcome' : di %9.2f (e(tau_bc))
	local se5_`outcome'    : di %9.2f (e(se_tau_rb))
	local pv5_`outcome'    : di %9.2f (e(pv_rb))
	local n5_`outcome'	   = e(N)
	local effn5_`outcome'  = e(N_h_l) + e(N_h_r)

	
}


* write table
texdoc init "${root}/results/tables/table_aggregate.tex", replace force

tex \caption{RD estimates of the effect of a left-wing mayor}
tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	Lame Duck 			& Tiebout $<$ median	 								& Ideology distance $>$ median  						& Oil windfall					\\
tex \midrule

tex \multicolumn{6}{c}{Size of city government} \\
tex \midrule
tex Expenditure per capita 					& `coeff1_tot_exp_avg_pc'`stars1_tot_exp_avg_pc' 				& `coeff2_tot_exp_avg_pc'`stars4_tot_exp_avg_pc' 				& `coeff3_tot_exp_avg_pc'`stars5_tot_exp_avg_pc' 				& `coeff4_tot_exp_avg_pc'`stars6_tot_exp_avg_pc' 				& `coeff5_tot_exp_avg_pc'`stars7_tot_exp_avg_pc' 	 			\\
tex  										& (`se1_tot_exp_avg_pc') 										& (`se2_tot_exp_avg_pc') 										& (`se3_tot_exp_avg_pc') 										& (`se4_tot_exp_avg_pc') 										& (`se5_tot_exp_avg_pc') 							 			\\
tex Expenditure, \% of GDP 					& `coeff1_tot_exp_avg_sgdp'`stars1_tot_exp_avg_sgdp' 			& `coeff2_tot_exp_avg_sgdp'`stars4_tot_exp_avg_sgdp' 			& `coeff3_tot_exp_avg_sgdp'`stars5_tot_exp_avg_sgdp' 		 	& `coeff4_tot_exp_avg_sgdp'`stars6_tot_exp_avg_sgdp' 		 	& `coeff5_tot_exp_avg_sgdp'`stars7_tot_exp_avg_sgdp' 			\\
tex  										& (`se1_tot_exp_avg_sgdp') 										& (`se2_tot_exp_avg_sgdp') 										& (`se3_tot_exp_avg_sgdp') 										& (`se4_tot_exp_avg_sgdp') 										& (`se5_tot_exp_avg_sgdp') 										\\
tex Revenue per capita 						& `coeff1_tot_rev_avg_pc'`stars1_tot_rev_avg_pc' 				& `coeff2_tot_rev_avg_pc'`stars4_tot_rev_avg_pc' 				& `coeff3_tot_rev_avg_pc'`stars5_tot_rev_avg_pc' 				& `coeff4_tot_rev_avg_pc'`stars6_tot_rev_avg_pc' 				& `coeff5_tot_rev_avg_pc'`stars7_tot_rev_avg_pc' 				\\
tex  										& (`se1_tot_rev_avg_pc') 										& (`se2_tot_rev_avg_pc') 										& (`se3_tot_rev_avg_pc') 										& (`se4_tot_rev_avg_pc') 										& (`se5_tot_rev_avg_pc') 										\\
tex Revenue, \% of GDP 						& `coeff1_tot_rev_avg_sgdp'`stars1_tot_rev_avg_sgdp' 			& `coeff2_tot_rev_avg_sgdp'`stars4_tot_rev_avg_sgdp' 			& `coeff3_tot_rev_avg_sgdp'`stars5_tot_rev_avg_sgdp' 			& `coeff4_tot_rev_avg_sgdp'`stars6_tot_rev_avg_sgdp' 			& `coeff5_tot_rev_avg_sgdp'`stars7_tot_rev_avg_sgdp' 			\\
tex  										& (`se1_tot_rev_avg_sgdp') 										& (`se2_tot_rev_avg_sgdp') 										& (`se3_tot_rev_avg_sgdp') 										& (`se4_tot_rev_avg_sgdp') 										& (`se5_tot_rev_avg_sgdp') 					 					\\
tex \midrule
tex \multicolumn{6}{c}{Allocation of resources: budget categories (\% of total expenditure)} \\
tex \midrule
tex Current Expenditure 					& `coeff1_current_exp_avg_share'`stars1_current_exp_avg_share' 	& `coeff2_current_exp_avg_share'`stars4_current_exp_avg_share' 	& `coeff3_current_exp_avg_share'`stars5_current_exp_avg_share' 	& `coeff4_current_exp_avg_share'`stars6_current_exp_avg_share' 	& `coeff5_current_exp_avg_share'`stars7_current_exp_avg_share' 	\\
tex  										& (`se1_current_exp_avg_share') 								& (`se2_current_exp_avg_share') 								& (`se3_current_exp_avg_share') 								& (`se4_current_exp_avg_share') 								& (`se5_current_exp_avg_share') 								\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm}  Personnel 								& `coeff1_personnel_avg_share'`stars1_personnel_avg_share' 		& `coeff2_personnel_avg_share'`stars4_personnel_avg_share' 		& `coeff3_personnel_avg_share'`stars5_personnel_avg_share' 		& `coeff4_personnel_avg_share'`stars6_personnel_avg_share' 		& `coeff5_personnel_avg_share'`stars7_personnel_avg_share' 		\\
tex  										& (`se1_personnel_avg_share') 									& (`se2_personnel_avg_share') 									& (`se3_personnel_avg_share') 									& (`se4_personnel_avg_share') 									& (`se5_personnel_avg_share') 									\\
tex Public Investment 						& `coeff1_investments_avg_share'`stars1_investments_avg_share' 	& `coeff2_investments_avg_share'`stars4_investments_avg_share' 	& `coeff3_investments_avg_share'`stars5_investments_avg_share' 	& `coeff4_investments_avg_share'`stars6_investments_avg_share' 	& `coeff5_investments_avg_share'`stars7_investments_avg_share' 	\\
tex  										& (`se1_investments_avg_share') 								& (`se2_investments_avg_share') 								& (`se3_investments_avg_share') 								& (`se4_investments_avg_share') 								& (`se5_investments_avg_share') 								\\
tex \midrule
tex \multicolumn{6}{c}{Allocation of resources: functional categories (\% of total expenditure)} \\
tex \midrule
tex Social Expenditures 					& `coeff1_social_exp_avg_share'`stars1_social_exp_avg_share' 	& `coeff2_social_exp_avg_share'`stars4_social_exp_avg_share' 	& `coeff3_social_exp_avg_share'`stars5_social_exp_avg_share' 	& `coeff4_social_exp_avg_share'`stars6_social_exp_avg_share' 	& `coeff5_social_exp_avg_share'`stars7_social_exp_avg_share' 	\\
tex  										& (`se1_social_exp_avg_share') 									& (`se2_social_exp_avg_share') 						 			& (`se3_social_exp_avg_share') 						 			& (`se4_social_exp_avg_share') 						 			& (`se5_social_exp_avg_share') 									\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Health \& sanitation 	& `coeff1_health_avg_share'`stars1_health_avg_share' 			& `coeff2_health_avg_share'`stars4_health_avg_share' 			& `coeff3_health_avg_share'`stars5_health_avg_share' 			& `coeff4_health_avg_share'`stars6_health_avg_share' 			& `coeff5_health_avg_share'`stars7_health_avg_share' 			\\
tex  										& (`se1_health_avg_share') 										& (`se2_health_avg_share') 										& (`se3_health_avg_share') 										& (`se4_health_avg_share') 										& (`se5_health_avg_share') 										\\
tex \hspace{0.20cm} Education \& culture 	& `coeff1_education_avg_share'`stars1_education_avg_share' 		& `coeff2_education_avg_share'`stars4_education_avg_share' 		& `coeff3_education_avg_share'`stars5_education_avg_share' 		& `coeff4_education_avg_share'`stars6_education_avg_share' 		& `coeff5_education_avg_share'`stars7_education_avg_share' 		\\
tex  										& (`se1_education_avg_share') 									& (`se2_education_avg_share') 									& (`se3_education_avg_share') 									& (`se4_education_avg_share') 									& (`se5_education_avg_share') 									\\
tex \hspace{0.20cm} Social welfare 			& `coeff1_welfare_avg_share'`stars1_welfare_avg_share' 			& `coeff2_welfare_avg_share'`stars4_welfare_avg_share' 			& `coeff3_welfare_avg_share'`stars5_welfare_avg_share' 			& `coeff4_welfare_avg_share'`stars6_welfare_avg_share' 			& `coeff5_welfare_avg_share'`stars7_welfare_avg_share' 			\\
tex  										& (`se1_welfare_avg_share') 									& (`se2_welfare_avg_share') 									& (`se3_welfare_avg_share') 									& (`se4_welfare_avg_share') 									& (`se5_welfare_avg_share') 									\\
*tex \midrule
tex \multicolumn{6}{l}{Other Expenditures:} \\
tex  \hspace{0.20cm} Housing 				& `coeff1_housing_avg_share'`stars1_housing_avg_share' 			& `coeff2_housing_avg_share'`stars4_housing_avg_share' 			& `coeff3_housing_avg_share'`stars5_housing_avg_share' 			& `coeff4_housing_avg_share'`stars6_housing_avg_share' 			& `coeff5_housing_avg_share'`stars7_housing_avg_share' 			\\
tex  										& (`se1_housing_avg_share') 									& (`se2_housing_avg_share') 									& (`se3_housing_avg_share') 									& (`se4_housing_avg_share') 									& (`se5_housing_avg_share') 									\\
tex  \hspace{0.20cm} Transportation 		& `coeff1_transport_avg_share'`stars1_transport_avg_share' 		& `coeff2_transport_avg_share'`stars4_transport_avg_share' 		& `coeff3_transport_avg_share'`stars5_transport_avg_share' 		& `coeff4_transport_avg_share'`stars6_transport_avg_share' 		& `coeff5_transport_avg_share'`stars7_transport_avg_share' 		\\
tex  										& (`se1_transport_avg_share') 									& (`se2_transport_avg_share') 									& (`se3_transport_avg_share') 									& (`se4_transport_avg_share') 									& (`se5_transport_avg_share') 									\\
tex  \hspace{0.20cm} Other 					& `coeff1_other_avg_share'`stars1_other_avg_share' 				& `coeff2_other_avg_share'`stars4_other_avg_share' 				& `coeff3_other_avg_share'`stars5_other_avg_share' 				& `coeff4_other_avg_share'`stars6_other_avg_share' 				& `coeff5_other_avg_share'`stars7_other_avg_share' 				\\
tex  										& (`se1_other_avg_share') 										& (`se2_other_avg_share') 										& (`se3_other_avg_share') 										& (`se4_other_avg_share') 										& (`se5_other_avg_share') 										\\
tex \midrule
tex Social Expenditures per capita 			& `coeff1_social_exp_avg_pc'`stars1_social_exp_avg_pc' 			& `coeff2_social_exp_avg_pc'`stars4_social_exp_avg_pc' 			& `coeff3_social_exp_avg_pc'`stars5_social_exp_avg_pc' 			& `coeff4_social_exp_avg_pc'`stars6_social_exp_avg_pc' 			& `coeff5_social_exp_avg_pc'`stars7_social_exp_avg_pc' 			\\
tex  										& (`se1_social_exp_avg_pc') 									& (`se2_social_exp_avg_pc') 						 			& (`se3_social_exp_avg_pc') 						 			& (`se4_social_exp_avg_pc') 						 			& (`se5_social_exp_avg_pc') 									\\
tex \bottomrule
tex Observations (all) 						&  `n1_tot_exp_avg_pc'    										& `n2_tot_exp_avg_pc'  											& `n3_tot_exp_avg_pc'  											& `n4_tot_exp_avg_pc'  											& `n5_tot_exp_avg_pc'  											\\
tex Observations (effective)				&  `effn1_tot_exp_avg_pc'										& `effn2_tot_exp_avg_pc'										& `effn3_tot_exp_avg_pc'   										& `effn4_tot_exp_avg_pc'										& `effn5_tot_exp_avg_pc'   										\\
tex \bottomrule
tex \end{tabularx}}

texdoc close
